package com.ttdj.cmel.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ttdj.cmel.CMELApplication;
import com.ttdj.cmel.R;
import com.umeng.analytics.MobclickAgent;

public class WebViewActivity extends BaseActivity {
    private static final String TAG = "WebViewActivity";
    private WebView webView;
    private String pauseUrl = "";
    private String blankUrl = "about:blank";

    @Override
    public int initContentView() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initUIAndListener() {
        webView = findViewById(R.id.home_web_view);
    }

    @Override
    protected void initData() {
        WebSettings settings = webView.getSettings();
//        webView.clearCache(true);
//        webView.clearHistory();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //支持通过JS打开新窗口
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportZoom(true);
        settings.setTextZoom(100);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setBlockNetworkImage(false);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
//        settings.setAppCacheMaxSize(1024*1024*8);
        String appCachePath = CMELApplication.getApplication().getCacheDir().getAbsolutePath();
        settings.setAppCachePath(appCachePath);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.e(TAG, "shouldOverrideUrlLoading");
                webView.loadUrl(request.getUrl().toString());
                return true;
            }
        });
        loadUrl();
    }

    @Override
    protected void onPause() {
        webView.reload();
        super.onPause();
        MobclickAgent.onPause(this);
//        webView.loadUrl(blankUrl);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webView == null) return;
        webView.removeAllViews();
        ((ViewGroup) webView.getParent()).removeView(webView);
        webView.setTag(null);
        webView.clearHistory();
        webView.destroy();
        webView = null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        /*if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack() && !webView.getUrl().equals(blankUrl)) {
                Log.e(TAG, "url=" + webView.getUrl());
                webView.goBack();
                return true;
            }
        }*/
        return super.onKeyDown(keyCode, event);
    }

    private void loadUrl() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("web_url");
        if (url != null && url.isEmpty()) return;
        webView.loadUrl(url);
        Log.d(TAG, "web_url=" + url);
    }

}
